package com.example.computer_exam_training_sys.controller;
import com.example.computer_exam_training_sys.entity.QuestionCommentInfo;
import com.example.computer_exam_training_sys.service.QuestionCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/question-comment") // 修改前缀为 /question-comment
@CrossOrigin
public class QuestionCommentController {
    @Autowired
    private QuestionCommentService commentService;

    @PostMapping("/add")
    public int addComment(@RequestBody QuestionCommentInfo comment) {
        return commentService.addComment(comment);
    }

    @DeleteMapping("/delete")
    public int deleteComment(@RequestParam Integer comment_id) {
        return commentService.deleteComment(comment_id);
    }

    @GetMapping("/findByQuestionId")
    public List<QuestionCommentInfo> getCommentsByQuestionId(@RequestParam Integer question_id) {
        return commentService.getCommentsByQuestionId(question_id);
    }

    @GetMapping("/list")
    public Map<String, Object> getComments(
            @RequestParam(required = false) String search,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return commentService.getCommentsWithPagination(search, page, size);
    }

    @PostMapping("/batchDelete")
    public int batchDeleteComments(@RequestBody Map<String, List<Integer>> request) {
        List<Integer> commentIds = request.get("commentIds");
        return commentService.batchDeleteComments(commentIds);
    }

    @GetMapping("/by-topic")
    public List<QuestionCommentInfo> getCommentsByTopicId(@RequestParam Integer topic_id) {
        return commentService.getCommentsByTopicId(topic_id);
    }
}